package net.htjs.juc.jmm;

import lombok.extern.slf4j.Slf4j;

/**
 * volatile和synchronized 都能保证内存的可见性
 * 区别:
 * 1.synchronized会创建 monitor，属于重量级操作，volatile是属于轻量级
 */
@Slf4j
public class RunSynchronizedDemo {

    static boolean run = true;
    static  Object lock = new Object();
    public static void main(String[] args) {
        new Thread(() -> {
            while(true) {
                synchronized (lock) {
                    if(!run) {
                        break;
                    }  else {
                         log.info("running...");
                    }
                }
            }

        }).start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        synchronized (lock) {
            log.info("end");
            run = false;
        }
    }
}
